package com.ca.ui.core.activity;

import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.viewpager2.widget.ViewPager2;

import com.ca.ui.core.R;
import com.ca.ui.core.adapter.BaseFragmentAdapter;
import com.ca.ui.core.view.fragment.ImageFragment;

public class ImageActivity extends ImmersiveActivity {

    private String[] imageUrlArray;
    private int index;

    private ViewPager2 pager;
    private BaseFragmentAdapter adapter;

    private TextView indexView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_image);

        initData();

        initView();

        initTransition();

        pager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                index = position;

                String text = (index + 1) + "/" + imageUrlArray.length;
                indexView.setText(text);
            }
        });
    }

    private void initData() {
        imageUrlArray = getIntent().getStringArrayExtra("imageUrlArray");
        if (imageUrlArray == null) {
            imageUrlArray = new String[0];
        }
        index = getIntent().getIntExtra("index", 0);
    }

    private void initView() {

        pager = findViewById(R.id.pager);
        adapter = new BaseFragmentAdapter(this);

        indexView = findViewById(R.id.tv_index);

        for (String url : imageUrlArray) {
            adapter.addFragment(new ImageFragment(this, url));
        }
        pager.setAdapter(adapter);

        pager.setCurrentItem(index);

        String text = (index + 1) + "/" + imageUrlArray.length;
        indexView.setText(text);
    }

    private void initTransition() {
        pager.setTransitionName("imageView");
    }
}